home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d14 / eyewatch.arc / EYES.C next >
Text File  |  1990-09-07  |  9KB  |  350 lines

  1. /********************************************************************\
  2.  EyeWatch -- Cursor Tracking Program.
  3.  
  4.  Written By:  Paul L. Yao & Malcolm Austin
  5. \********************************************************************/
  6.  
  7.  
  8. #include "windows.h"
  9. #include <math.h>
  10.  
  11. #define IRIS_LEFT   (-22)
  12. #define IRIS_RIGHT  ( 22)
  13. #define IRIS_TOP    ( 19)
  14. #define IRIS_BOTTOM (-19)
  15. #define ID_ABOUT    100
  16.  
  17. static  HANDLE  hInst;
  18. static  HWND  hWnd;
  19.  
  20. POINT pt;
  21. POINT ptOld;
  22.  
  23. RECT  rOldEye1;
  24. RECT  rOldEye2;
  25.  
  26. HBRUSH hWhiteBrush;
  27. HBRUSH hBlackBrush;
  28. HPEN   hWhitePen;
  29. HBRUSH hBackgroundBrush;
  30.  
  31. char   acEyeWndProc[]  = "PlyEyes";
  32.  
  33. long FAR PASCAL MainWinProc (HWND, unsigned, WORD, LONG);
  34. long FAR PASCAL EyeWinProc (HWND, unsigned, WORD, LONG);
  35. BOOL FAR PASCAL AboutboxWindowProc (HWND, unsigned, WORD, LONG);
  36. void NEAR PASCAL DrawEyes (HDC, HWND);
  37. void NEAR PASCAL DrawEyeLid (HDC, HWND);
  38.  
  39. int PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  40. HANDLE hInstance, hPrevInstance;
  41. LPSTR  lpszCmdLine;
  42. int    cmdShow;
  43. {
  44.   MSG   msg;
  45.   RECT  r;
  46.  
  47.   if (!hPrevInstance)
  48.   {
  49.     WNDCLASS   rClass;
  50.  
  51.     rClass.lpszClassName = acEyeWndProc;
  52.     rClass.hInstance     = hInstance;
  53.     rClass.lpfnWndProc     = EyeWinProc;
  54.     rClass.hCursor       = LoadCursor(NULL, IDC_ARROW) ;
  55.     rClass.hIcon         = NULL;
  56.     rClass.lpszMenuName  = NULL;
  57.     rClass.hbrBackground = NULL;
  58.     rClass.style     = CS_HREDRAW|CS_VREDRAW;
  59.     rClass.cbClsExtra    = 0;
  60.     rClass.cbWndExtra    = 0;
  61.  
  62.     RegisterClass(&rClass);
  63.  
  64.   }
  65.   else ;
  66.  
  67.   hInst = hInstance;
  68.  
  69.   hWhiteBrush = GetStockObject (WHITE_BRUSH);
  70.   hBlackBrush = GetStockObject (BLACK_BRUSH);
  71.   hWhitePen   = GetStockObject (WHITE_PEN);
  72.  
  73.   hBackgroundBrush = CreateSolidBrush (GetSysColor(COLOR_BACKGROUND));
  74.  
  75.   hWnd = CreateWindow(acEyeWndProc,
  76.               "Eyes",
  77.               WS_OVERLAPPED  |
  78.               WS_CAPTION     |
  79.               WS_SYSMENU     |
  80.                       WS_MINIMIZEBOX |
  81.               WS_THICKFRAME,
  82.                       CW_USEDEFAULT,
  83.                       0,
  84.                       CW_USEDEFAULT,
  85.                       0,
  86.               NULL,
  87.               NULL,
  88.               hInstance,
  89.               NULL
  90.                      );
  91.  
  92.   ShowWindow(hWnd, SW_MINIMIZE);
  93.  
  94.   SetTimer (hWnd, 1, 100, NULL);
  95.  
  96.   while (GetMessage(&msg, NULL, 0, 0))
  97.   {
  98.     TranslateMessage(&msg);
  99.     DispatchMessage(&msg);
  100.   }
  101.  
  102.   DeleteObject (hBackgroundBrush);
  103.  
  104.   exit(msg.wParam);
  105.  
  106. }
  107.  
  108. /********************************************************************\
  109. \********************************************************************/
  110. long FAR PASCAL EyeWinProc (hWnd, msg, wParam, lParam)
  111. HWND       hWnd;
  112. unsigned   msg;
  113. WORD       wParam;
  114. LONG       lParam;
  115. {
  116.   HDC hDC;
  117.   PAINTSTRUCT ps;
  118.   RECT r;
  119.   HDC  hDCBitmap;
  120.   HBITMAP hBitmap, hOldBitmap;
  121.   FARPROC  lpprocAbout;
  122.  
  123.   switch (msg)
  124.   {
  125.     case WM_SYSCOMMAND:
  126.     switch (wParam)
  127.     {
  128.     case ID_ABOUT:
  129.         lpprocAbout = MakeProcInstance( AboutboxWindowProc, hInst);
  130.         DialogBox (hInst, "AboutBox", hWnd, lpprocAbout);
  131.         FreeProcInstance ( lpprocAbout);
  132.         break;
  133.     default:
  134.         return(DefWindowProc(hWnd, msg, wParam, lParam));
  135.         break;
  136.     }
  137.     break;
  138.  
  139.     case WM_CREATE:
  140.     {
  141.     HWND hSystMenu;
  142.     hSystMenu = GetSystemMenu(hWnd, FALSE);
  143.     ChangeMenu (hSystMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  144.     ChangeMenu (hSystMenu, 0, "About...", ID_ABOUT, MF_APPEND | MF_STRING);
  145.     }
  146.     break;
  147.  
  148.     case WM_CLOSE:
  149.         DestroyWindow (hWnd);
  150.         break;
  151.  
  152.     case WM_DESTROY:
  153.         PostQuitMessage(0);
  154.         break;
  155.  
  156.     case WM_ERASEBKGND:
  157.         GetClientRect (hWnd, &r);
  158.     DPtoLP ((HDC)wParam, (LPPOINT)&r, 2);
  159.     FillRect ((HDC)wParam, &r, hBackgroundBrush);
  160.  
  161.     DrawEyeLid ((HDC)wParam, hWnd);
  162.     return (1L);
  163.         break;
  164.  
  165.     case WM_GETMINMAXINFO:
  166.     {
  167.     LPPOINT lp;
  168.  
  169.     lp = (LPPOINT)lParam + 3;
  170.  
  171.     /* Set minimum tracking size.  */
  172.     lp-> x =  GetSystemMetrics (SM_CXMINTRACK)/2;
  173.     }
  174.     break;
  175.  
  176.     case WM_PAINT:
  177.     hDC = BeginPaint (hWnd, &ps);
  178.     DrawEyes (hDC, hWnd);
  179.     EndPaint (hWnd, &ps);
  180.         break;
  181.  
  182.     case WM_TIMER:
  183.         GetCursorPos (&pt);
  184.         if (ptOld.x != pt.x || ptOld.y != pt.y ) {
  185.         hDC = GetDC (hWnd);
  186.         GetClientRect (hWnd, &r);
  187.         if (r.right > 200 || r.bottom > 100) {
  188.         DrawEyes (hDC, hWnd);
  189.         goto CleanUp;
  190.         }
  191.         hDCBitmap = CreateCompatibleDC (hDC);
  192.         hBitmap   = CreateCompatibleBitmap (hDC, r.right, r.bottom);
  193.         if (hBitmap) {
  194.         hOldBitmap = SelectObject (hDCBitmap, hBitmap);
  195.         FillRect (hDCBitmap, &r, hBackgroundBrush);
  196.         DrawEyeLid (hDCBitmap, hWnd);
  197.         DrawEyes (hDCBitmap, hWnd);
  198.         SetMapMode (hDCBitmap, MM_TEXT);
  199.         SetViewportOrg (hDCBitmap, 0, 0);
  200.         BitBlt (hDC, 0, 0, r.right, r.bottom,
  201.             hDCBitmap, 0, 0, SRCCOPY);
  202.         SelectObject (hDCBitmap, hOldBitmap);
  203.         DeleteObject (hBitmap);
  204.         DeleteDC (hDCBitmap);
  205.         }
  206.         else {
  207.         DeleteDC (hDCBitmap);
  208.         DrawEyes (hDC, hWnd);
  209.         }
  210. CleanUp:
  211.         ReleaseDC (hWnd, hDC);
  212.             ptOld.x = pt.x;
  213.             ptOld.y = pt.y;
  214.             }
  215.         break;
  216.  
  217.     default:
  218.       return(DefWindowProc(hWnd, msg, wParam, lParam));
  219.       break;
  220.   }
  221.   return(0L);
  222. }
  223.  
  224. void NEAR PASCAL DrawEyes (HDC hDC, HWND hWnd)
  225. {
  226.   RECT r;
  227.   POINT pt2;
  228.   int   xShift;
  229.   int   yShift;
  230.   double dX;
  231.   double dY;
  232.   double dT;
  233.  
  234.     GetClientRect (hWnd, &r);
  235.     SetMapMode (hDC, MM_ISOTROPIC);
  236.     SetWindowExt (hDC, 200, 100);
  237.     SetViewportExt (hDC, r.right, r.bottom);
  238.  
  239.     /*    Don't use pen for drawing eyes.  */
  240.     SelectObject (hDC, hWhitePen);
  241.     SelectObject (hDC, hWhiteBrush);
  242.  
  243.     /****************** Left Eye.  ***********************/
  244.  
  245.     /* Erase old eye.  */
  246.  
  247.     SetViewportOrg (hDC, r.right/4, r.bottom/2);
  248.     Ellipse (hDC, rOldEye1.left,  rOldEye1.top,
  249.           rOldEye1.right, rOldEye1.bottom);
  250.  
  251.     /* Determine location of iris.  */
  252.     pt2 = pt;
  253.     ScreenToClient (hWnd, &pt2);
  254.     DPtoLP (hDC, &pt2, 1);
  255.  
  256.     dX = (double) pt2.x;
  257.     dY = (double) pt2.y;
  258.     dT = sqrt ((dX*dX)+(dY*dY));
  259.  
  260.     xShift = (int) ( (IRIS_RIGHT<dT) ? (dX * IRIS_RIGHT/dT) : dX );
  261.     yShift = (int) ( (IRIS_TOP<dT) ? (dY * IRIS_TOP/dT) : dY );
  262.  
  263.     /* Draw iris.  */
  264.     SelectObject (hDC, hBlackBrush);
  265.  
  266.     rOldEye1.left   = IRIS_LEFT  +xShift;
  267.     rOldEye1.top    = IRIS_TOP     +yShift;
  268.     rOldEye1.right  = IRIS_RIGHT +xShift;
  269.     rOldEye1.bottom = IRIS_BOTTOM+yShift;
  270.     Ellipse (hDC, rOldEye1.left,  rOldEye1.top,
  271.           rOldEye1.right, rOldEye1.bottom);
  272.  
  273.     /*********************** Right Eye. ************************/
  274.  
  275.     /* Erase old eye.  */
  276.     SelectObject (hDC, hWhiteBrush);
  277.  
  278.     SetViewportOrg (hDC, (3*r.right)/4, r.bottom/2);
  279.     Ellipse (hDC, rOldEye2.left,  rOldEye2.top,
  280.           rOldEye2.right, rOldEye2.bottom);
  281.  
  282.     /* Determine location of iris.  */
  283.     pt2 = pt;
  284.     ScreenToClient (hWnd, &pt2);
  285.     DPtoLP (hDC, &pt2, 1);
  286.  
  287.     dX = (double) pt2.x;
  288.     dY = (double) pt2.y;
  289.     dT = sqrt ((dX*dX)+(dY*dY));
  290.  
  291.     xShift = (int) ( (IRIS_RIGHT<dT) ? (dX * IRIS_RIGHT/dT) : dX );
  292.     yShift = (int) ( (IRIS_TOP<dT) ? (dY * IRIS_TOP/dT) : dY );
  293.  
  294.     /* Draw iris.  */
  295.     SelectObject (hDC, hBlackBrush);
  296.  
  297.     rOldEye2.left   = IRIS_LEFT  +xShift;
  298.     rOldEye2.top    = IRIS_TOP     +yShift;
  299.     rOldEye2.right  = IRIS_RIGHT +xShift;
  300.     rOldEye2.bottom = IRIS_BOTTOM+yShift;
  301.     Ellipse (hDC, rOldEye2.left,  rOldEye2.top,
  302.           rOldEye2.right, rOldEye2.bottom);
  303.  
  304.     }
  305.  
  306.  
  307. void NEAR PASCAL DrawEyeLid (HDC hDC, HWND hWnd)
  308. {
  309. RECT r;
  310.  
  311.     /* Set up mapping mode. */
  312.     GetClientRect (hWnd, &r);
  313.     SetMapMode (hDC, MM_ISOTROPIC);
  314.     SetWindowExt (hDC, 200, 100);
  315.     SetViewportExt (hDC, r.right, r.bottom);
  316.  
  317.     /* Draw Outline of Left Eye.  */
  318.     SetViewportOrg (hDC, r.right/4, r.bottom/2);
  319.     Ellipse (hDC, -49, 42, 49, -42);
  320.  
  321.     /* Draw outline of Right eye. */
  322.     SetViewportOrg (hDC, (3*r.right)/4, r.bottom/2);
  323.     Ellipse (hDC, -49, 42, 49, -42);
  324. }
  325.  
  326.  
  327. /***************************************************************************/
  328. /*    T H E   A B O U T   B O X   H A N D L I N G   P R O C E D U R E      */
  329. /***************************************************************************/
  330.  
  331. /*   (This is the window procedure for the About dialog box.)              */
  332.  
  333.  
  334. BOOL FAR PASCAL AboutboxWindowProc (hDlg, message, wParam, lParam)
  335.  
  336. HWND            hDlg;
  337. unsigned        message;
  338. WORD            wParam;
  339. LONG            lParam;
  340.  
  341. {
  342.     if (message == WM_COMMAND) {
  343.         EndDialog (hDlg, TRUE);
  344.         return TRUE;
  345.         }
  346.     else if (message == WM_INITDIALOG)
  347.        return TRUE;
  348.     else return FALSE;
  349. }
  350.